/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pA = headA,pB = headB;
        while(pA!=pB) {
            if(pA!=null) {
                pA = pA.next;
            } else {
                pA = headB;
            }
            if(pB!=null) {
                pB = pB.next;
            } else {
                pB = headA;
            }
        }
        return pA;
    }
}